package leetcode.N1_N100;

import java.util.Objects;
import java.util.Stack;

public class N20 {

    public static void main(String[] args) {

        System.out.println(isValid("((())))"));

    }

    static boolean isValid(String s) {
        Stack<Character> a = new Stack<>();
        for (int i = 0, length = s.length(); i < length; i++) {
            Character c = s.charAt(i);
            if (a.empty() || !Objects.equals(a.peek(), reversal(c))) {
                a.push(c);
            } else {
                a.pop();
            }
        }
        return 0 == a.size();
    }

    static Character reversal(Character c) {
        Character result = null;
        if (c == '(') {
            result = ')';
        } else if (c == ')') {
            result = '(';
        } else if (c == '[') {
            result = ']';
        } else if (c == ']') {
            result = '[';
        } else if (c == '{') {
            result = '}';
        } else if (c == '}') {
            result = '{';
        }
        return result;
    }


}
